package ${config.controlPackage};

import ${config.modulePackage}.${info.className}${config.moduleSuffix};
import ${config.servicePackage}.${info.className}${config.serviceSuffix};
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
* 描述：${info.tableComment}业务控制
* @author ${config.author}
* @date ${.now}
*/
@RequestMapping(value = "/${info.className?uncap_first}")
@RestController
@Validated
public class ${info.className}${config.controlSuffix}{
    private static final Logger logger = LoggerFactory.getLogger(${info.className}${config.controlSuffix}.class);

    @Autowired
    private ${info.className}${config.serviceSuffix} ${info.className?uncap_first}${config.serviceSuffix};

    /**
     * 创建${info.tableComment}
     * @param ${info.className?uncap_first} ${info.tableComment}
     */
    @PostMapping
    public void create(@RequestBody @Valid ${info.className}${config.moduleSuffix} ${info.className?uncap_first}) {
        ${info.className?uncap_first}${config.serviceSuffix}.save(${info.className?uncap_first});
    }

    /**
     * 修改${info.tableComment}
     * @param ${info.className?uncap_first} ${info.tableComment}
     */
    @PutMapping
    public void update(@RequestBody @Valid ${info.className}${config.moduleSuffix} ${info.className?uncap_first}) {
        ${info.className?uncap_first}${config.serviceSuffix}.updateById(${info.className?uncap_first});
    }

    /**
     * 获取${info.tableComment}
     * @param id ${info.tableComment}主键
     */
    @GetMapping("/{id}")
    public void select(@PathVariable("id") String id) {
        ${info.className}${config.moduleSuffix} ${info.className?uncap_first} = ${info.className?uncap_first}${config.serviceSuffix}.getById(id);
        if (Objects.isNull(${info.className?uncap_first})){
            throw new RuntimeException("当前${info.tableComment}不存在");
        }
        return ${info.className?uncap_first};
    }

    /**
     * 获取${info.tableComment}
     * @param cond ${info.tableComment}主键
     */
    @PostMapping("/page")
    public void page(@RequestBody @Valid Condition<${info.className}${config.moduleSuffix}> cond) {
        ${info.className?uncap_first}${config.serviceSuffix}.page(cond.getPage(), cond.getCond())
    }

    /**
     * 删除${info.tableComment}
     * @param id ${info.tableComment}主键
     */
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") String id) {
        ${info.className?uncap_first}${config.serviceSuffix}.removeById(id)
    }
}